﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeManageService;

namespace WeManageForm
{
    public partial class FindPasswordForm : Form
    {
        public Point mouseDownPoint;
        public UserManager userManager = new UserManager();
        public User user;

        public FindPasswordForm(string userName)
        {
            InitializeComponent();
            TxtUserName.Text = userName;
        }

        private void BtnFind_Click(object sender, EventArgs e)
        {            
            TxtOldPassword.Text = userManager.FindPassword(TxtUserName.Text);
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FindPasswordForm_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDownPoint = new Point(e.X, e.Y);
        }

        private void FindPasswordForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mouseDownPoint.X,
                    this.Location.Y + e.Y - mouseDownPoint.Y);
            }
        }

        private void BtnModify_Click(object sender, EventArgs e)
        {
            if(TxtPassword.Text != "" && TxtConfirmPassword.Text == TxtPassword.Text)
            {
                User user = userManager.GetUser(TxtUserName.Text);
                if (user != null)
                {
                    user.PassWord = TxtPassword.Text;
                    userManager.ModifyUser(user);
                    MessageBox.Show("修改成功", "");
                    TxtConfirmPassword.Text = "";
                    TxtPassword.Text = "";
                }
            }
        }

        private void TxtComfirmPassword_TextChanged(object sender, EventArgs e)
        {
            if (TxtPassword.Text == "" || TxtConfirmPassword.Text == TxtPassword.Text)
            {
                LblAlertConfirm.Text = "";
            }
            else
            {
                LblAlertConfirm.Text = "两次密码不相同！";
            }
        }

        private void TxtPassword_TextChanged(object sender, EventArgs e)
        {
            LblAlertPassword.Text = "";
            if (TxtPassword.Text == "" || TxtConfirmPassword.Text == TxtPassword.Text)
            {
                LblAlertConfirm.Text = "";
            }
        }
    }
}
